{
 "cells": [
  {
   "cell_type": "code",
   "id": "3eac46e1e874435d",
   "metadata": {},
   "source": [
    "import akshare as ak\n",
    "\n",
    "# 获取单一可转债信息\n",
    "bond_cb_profile_sina_df = ak.bond_cb_profile_sina(symbol=\"sz128039\")\n",
    "print(bond_cb_profile_sina_df)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import akshare as ak\n",
    "\n",
    "# 全市场所有可转债戏信息(不全)\n",
    "ak.bond_zh_cov()"
   ],
   "id": "d805593efeb1c9c7",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    " # 单次返回所有沪深可转债的实时行情数据\n",
    "ak.bond_zh_hs_cov_spot()"
   ],
   "id": "85b4fbae931766d4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import akshare as ak\n",
    "\n",
    "# code =\"sz123205\"\n",
    "code = \"sh113555\"\n",
    "# 转债历史行情数据\n",
    "df = ak.bond_zh_hs_cov_daily(symbol=code)\n",
    "#print(df)\n",
    "df.to_csv(f\"./test/{code}.csv\")"
   ],
   "id": "3842c8a3769eef51",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import efinance as ef\n",
    "\n",
    "# 目前市场的可转债信息\n",
    "ef.bond.get_realtime_quotes()"
   ],
   "id": "925945566504c856",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 指定可转债k线信息\n",
    "import efinance as ef\n",
    "\n",
    "# 可转债代码（以 东财转3 为例）\n",
    "bond_code = 'sh110026'\n",
    "ef.bond.get_quote_history(bond_code)"
   ],
   "id": "6938341eadb8b55",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import efinance as ef\n",
    "\n",
    "# 获取全市场可转债基础信息\n",
    "pf = ef.bond.get_all_base_info()\n",
    "# 解决乱码问题：使用GBK编码（Windows系统默认），并处理特殊字符\n",
    "pf.to_csv(\n",
    "    \"./筛选后的可转债.csv\",\n",
    "    index=False,\n",
    "    encoding='gbk',  # 改为GBK编码适应Windows系统\n",
    "    errors='ignore'  # 忽略无法编码的特殊字符\n",
    ")\n",
    "print(f\"成功保存 {len(pf)} 条可转债数据，已处理编码问题\")"
   ],
   "id": "858c69e9239182b4",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "95577a35c6d6a505",
   "metadata": {},
   "source": [
    "# 模拟浏览器的获取html信息\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.service import Service as ChromeService\n",
    "from selenium.webdriver.support.ui import WebDriverWait\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "\n",
    "# 设置驱动路径\n",
    "service = ChromeService(executable_path=\"C:\\\\Program Files\\\\Google\\\\Chrome\\\\Application\\\\chromedriver.exe\")\n",
    "options = webdriver.ChromeOptions()\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "\n",
    "driver.get(\"https://www.baidu.com\")\n",
    "driver.maximize_window()\n",
    "\n",
    "try:\n",
    "    # 调试用：等待 2 秒，保证页面加载\n",
    "    time.sleep(2)\n",
    "\n",
    "    try:\n",
    "        search_box = driver.find_element(By.ID, \"kw\")\n",
    "        driver.execute_script(\"arguments[0].value='Selenium Python';\", search_box)\n",
    "\n",
    "    except Exception as e:\n",
    "        print(\"❌ 没找到搜索框，打印页面源码前 500 个字符：\")\n",
    "        print(driver.page_source[:500])  # 打印前 500 个字符\n",
    "        print(\"错误详情：\", str(e))\n",
    "\n",
    "    # 尝试点击搜索按钮\n",
    "    try:\n",
    "        search_button = WebDriverWait(driver, 10).until(\n",
    "            EC.presence_of_element_located((By.ID, \"su\"))\n",
    "        )\n",
    "        driver.execute_script(\"arguments[0].click();\", search_button)\n",
    "        print(\"✅ 点击搜索按钮成功！\")\n",
    "        time.sleep(3)\n",
    "        print(\"⏳ 正在等待 1 分钟...\")\n",
    "        time.sleep(60)\n",
    "\n",
    "    except Exception as e:\n",
    "        print(\"❌ 没找到搜索按钮：\", str(e))\n",
    "\n",
    "finally:\n",
    "    driver.quit()\n"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "87dee90d",
   "metadata": {},
   "source": [
    "#免息期46天,假设我每天借钱100元  ,借钱的时间和年华利率的关系\n",
    "money = 100\n",
    "rate_money = 0\n",
    "rate = 0.006\n",
    "day = 58\n",
    "for i in range(1, day):\n",
    "    rate_money = money * rate\n",
    "    year_rate = rate_money / ((money - rate_money) * (day - i))\n",
    "    print(f\"借钱{i}天,免息期{(day - i)}天,借款金额{money},支付利息{rate_money},年华利率{year_rate * 365 * 100}\")\n",
    "\n"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import schedule\n",
    "import time\n",
    "import datetime\n",
    "\n",
    "\n",
    "def job():\n",
    "    \"\"\"要定时执行的任务\"\"\"\n",
    "    print(f\"任务执行时间: {datetime.datetime.now()}\")\n",
    "    # 在这里编写你的业务逻辑\n",
    "\n",
    "\n",
    "# 每天固定时执行，例如每天10:30执行\n",
    "schedule.every().minute.at(\":00\").do(job)\n",
    "# schedule.every().day.at(\"10:30\").do(job)\n",
    "\n",
    "print(\"定时 task started...\")\n",
    "while True:\n",
    "    schedule.run_pending()  # 运行所有可以运行的任务\n",
    "    time.sleep(1)  # 休眠1秒，减少CPU占用"
   ],
   "id": "e2f8d517831d361a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    " # 计算两个日期之间的天数\n",
    "from datetime import date\n",
    "\n",
    "\n",
    "def pingan():\n",
    "    print(\"-------------平安银行---------------\")\n",
    "    for i in range(1, 13):\n",
    "        if i == 2:\n",
    "            target_date = date(2025, 3, 1)  # 账单计算起始日\n",
    "        else:\n",
    "            target_date = date(2025, i, 29)\n",
    "        if i == 11:\n",
    "            today = date(2026, 1, 19)\n",
    "        elif i == 12:\n",
    "            today = date(2026, 2, 19)\n",
    "        else:\n",
    "            today = date(2025, i + 2, 19)  # 最迟还款日\n",
    "\n",
    "        days_left = abs((target_date - today).days)\n",
    "        print(f\"刷卡时间：{target_date}，还款时间：{today}，免息时间 {days_left} 天\")\n",
    "\n",
    "\n",
    "def zhongxin():\n",
    "    print(\"-------------中信银行---------------\")\n",
    "    for i in range(1, 13):\n",
    "\n",
    "        target_date = date(2025, i, 21)  # 账单计算起始日\n",
    "\n",
    "        if i == 11:\n",
    "            today = date(2026, 1, 12)\n",
    "        elif i == 12:\n",
    "            today = date(2026, 2, 12)\n",
    "        else:\n",
    "            today = date(2025, i + 2, 12)  # 最迟还款日\n",
    "\n",
    "        days_left = abs((target_date - today).days)\n",
    "        print(f\"刷卡时间：{target_date}，还款时间：{today}，免息时间 {days_left} 天\")\n",
    "\n",
    "\n",
    "def zhaoshang():\n",
    "    print(\"-------------招商银行---------------\")\n",
    "    for i in range(1, 13):\n",
    "\n",
    "        target_date = date(2025, i, 21)  # 账单计算起始日\n",
    "\n",
    "        if i == 11:\n",
    "            today = date(2026, 1, 11)\n",
    "        elif i == 12:\n",
    "            today = date(2026, 2, 11)\n",
    "        else:\n",
    "            today = date(2025, i + 2, 11)  # 最迟还款日\n",
    "\n",
    "        days_left = abs((target_date - today).days)\n",
    "        print(f\"刷卡时间：{target_date}，还款时间：{today}，免息时间 {days_left} 天\")\n",
    "\n",
    "def jiaotong():\n",
    "    print(\"-------------交通银行---------------\")\n",
    "    for i in range(1, 13):\n",
    "\n",
    "        target_date = date(2025, i, 21)  # 账单计算起始日\n",
    "\n",
    "        if i == 11:\n",
    "            today = date(2026, 1, 17)\n",
    "        elif i == 12:\n",
    "            today = date(2026, 2, 17)\n",
    "        else:\n",
    "            today = date(2025, i + 2, 17)  # 最迟还款日\n",
    "\n",
    "        days_left = abs((target_date - today).days)\n",
    "        print(f\"刷卡时间：{target_date}，还款时间：{today}，免息时间 {days_left} 天\")\n",
    "\n",
    "# zhaoshang()\n",
    "# zhongxin()\n",
    "# pingan()\n",
    "jiaotong()\n"
   ],
   "id": "8fdbfc25cbc8d053",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T16:26:21.612323Z",
     "start_time": "2025-10-20T16:26:20.895769Z"
    }
   },
   "cell_type": "code",
   "source": [
    " import akshare as ak\n",
    " ak.bond_zh_us_rate(start_date='20251020')"
   ],
   "id": "8556256c28ae3d14",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  0%|          | 0/19 [00:00<?, ?it/s]"
      ],
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "b0bde6ef07bb49d9a2cb6504e477f4b5"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "           日期  中国国债收益率2年  中国国债收益率5年  中国国债收益率10年  中国国债收益率30年  中国国债收益率10年-2年  \\\n",
       "0  2025-10-20     1.5014     1.6104      1.8498      2.2162         0.3484   \n",
       "\n",
       "   中国GDP年增率  美国国债收益率2年  美国国债收益率5年  美国国债收益率10年  美国国债收益率30年  美国国债收益率10年-2年  \\\n",
       "0       NaN        NaN        NaN         NaN         NaN            NaN   \n",
       "\n",
       "   美国GDP年增率  \n",
       "0       NaN  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>中国国债收益率2年</th>\n",
       "      <th>中国国债收益率5年</th>\n",
       "      <th>中国国债收益率10年</th>\n",
       "      <th>中国国债收益率30年</th>\n",
       "      <th>中国国债收益率10年-2年</th>\n",
       "      <th>中国GDP年增率</th>\n",
       "      <th>美国国债收益率2年</th>\n",
       "      <th>美国国债收益率5年</th>\n",
       "      <th>美国国债收益率10年</th>\n",
       "      <th>美国国债收益率30年</th>\n",
       "      <th>美国国债收益率10年-2年</th>\n",
       "      <th>美国GDP年增率</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2025-10-20</td>\n",
       "      <td>1.5014</td>\n",
       "      <td>1.6104</td>\n",
       "      <td>1.8498</td>\n",
       "      <td>2.2162</td>\n",
       "      <td>0.3484</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "f4f4a2e3c9ba59fa"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
